import fetch from 'common/src/utils/fetch';
import UTILS from "common/src/utils/utils";
import axios from 'axios';

/**
 * 新增免修免考设置
 */
 function addExemptionAuditSwitch(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/auditSwitch',
        method: 'post',
        data: dataArrToString(data)
    });
}

/**
 * 修改免修免考设置
 */
 function updateExemptionAuditSwitch(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/auditSwitch',
        method: 'put',
        data: dataArrToString(data)
    });
}

/**
 * 查询免修免考设置
 */
 function getExemptionAuditSwitchById(id) {
    return fetch({
        url: `/api/electionservice/exemptionGradute/auditSwitch/${id}`,
        method: 'get'
    });
}

/**
 * 删除免修免考设置
 */
 function deleteExemptionAuditSwitch(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/auditSwitch',
        method: 'delete',
        data: data
    });
}

/**
 * 新增免修免考条件
 */
 function addExemptionApplyCondition(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/applyCondition',
        method: 'post',
        data: dataArrToString(data)
    });
}

/**
 * 修改免修免考条件
 */
 function updateExemptionApplyCondition(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/applyCondition',
        method: 'put',
        data: dataArrToString(data)
    });
}

/**
 * 查询免修免考条件
 */
 function getExemptionApplyConditionById(id) {
    return fetch({
        url: `/api/electionservice/exemptionGradute/applyCondition/${id}`,
        method: 'get'
    });
}

/**
 * 删除免修免考条件
 */
function deleteExemptionApplyCondition(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/applyCondition',
        method: 'delete',
        data: data
    });
}

/**
 * 下载免修免考条件模板
 */
function downloadApplyConditionTemplate(){
    window.location.href = `/api/electionservice/exemptionGradute/applyCondition/export`
}

/**
 * 免修免考审核
 */
 function updateExemptionAuditStatus(data) {
    return fetch({
        url: '/api/electionservice/exemption/approvalGraduteExemptionApply?status=' + data.status,
        method: 'post',
        data: data.ids
    });
}

/**
 * 新增免修申请（免审和）
 */
 function addExemptionApplyAdmin(data) {
    return fetch({
        url: '/api/electionservice/exemption/adminAddApply',
        method: 'post',
        data: data
    });
}

/**
 * 删除免修申请
 */
 function deleteExemptionApplyAdmin(ids) {
    return fetch({
        url: '/api/electionservice/exemption/deleteExemptionApply',
        method: 'post',
        data: ids
    });
}

/**
 * 导出免修审核
 */
function exportExemptionAudit(data){
    return fetch({
        url: `api/electionservice/exemption/findGraduateExemptionApplyExport`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, 'YanJiuShengMianXiuMianKaoShenHeLieBiao.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}

/**
 * 导出免修统计
 */
function exportExemptionStatistics(data){
    return fetch({
        url: `/api/electionservice/exemption/exemptionCountExport`,
        method: 'post',
        data: data,
        responseType: 'blob'
    }).then(res => {
        UTILS.downloadFile(res, 'MianXiuMianKaoTongJi.xls', 'application/vnd.ms-excel;charset=utf-8');
    });
}

/**
 * 根据code查询课程信息
 */
function getCourseInfoByCode(code) {
    return fetch({
        url: `/api/electionservice/exemptionGradute/applyCondition/courseInfo/${code}`,
        method: 'post'
    });
}

/**
 * 根据学生code查询课程
 */
function getCourseByStudentCode(data) {
    let queryParam = ''
    for (const key in data) {
        if (data[key] == '') {
            continue
        }
        queryParam += `${key}=${data[key]}&`
    }
    return fetch({
        url: `api/electionservice/exemption/findCourseAdd?${queryParam}`,
        method: 'get'
    });
}

/**
 * 根据课程code和学籍信息查询所有符合的申请条件
 */
function getMatchedConditionsByCode(data) {
    return fetch({
        url: '/api/electionservice/exemptionGradute/applyCondition/matchedConditions',
        method: 'post',
        data: data
    });
}

/**
 * 学生免修课程列表
 */
function getExemptionApplyCourse(data) {
    return fetch({
        url: '/api/electionservice/exemption/findStudentCourse?calendarId=' + data.calendarId,
        method: 'get'
    });
}

/**
 * 学生申请免修课程
 */
function addExemptionApply(data) {
    return fetch({
        url: '/api/electionservice/exemption/addGraduteExemptionApply',
        method: 'post',
        data: data
    });
}

function dataArrToString(data) {
  let newData = Object.assign({}, data);
  for(let key in newData) {
    if(newData[key] instanceof Array) {
      newData[key] = newData[key].join(",");
    }
  }
  return newData;
}

/**
 * 下载证明材料
 */
function downloadExamApply(data) {
    return fetch({
      url: "/api/electionservice//exemption/exportExemptionListZip",
      method: "post",
      data: data
    });
  }
  
  /**
   * 循环获取key
   */
  function getResultByObsKey(key) {
    return fetch({
      url: "/api/electionservice/graduateExamMessage/resultObs/"+ key,
      method: "get"
    });
  }

  /**
   * 撤销申请
   */
  function cancelGraduteExemptionApply(data) {
    return fetch({
      url: "/api/electionservice/exemption/cancelGraduteExemptionApply/",
      method: "post",
      data
    });
  }

export default {
    downloadExamApply: downloadExamApply,
    getResultByObsKey: getResultByObsKey,
    "getCourseInfoByCode": getCourseInfoByCode,
    "getCourseByStudentCode": getCourseByStudentCode,

    "getExemptionAuditSwitchUrl": '/api/electionservice/exemptionGradute/auditSwitch/list',  //免修设置列表（设置）
    "addExemptionAuditSwitch": addExemptionAuditSwitch,
    "updateExemptionAuditSwitch": updateExemptionAuditSwitch,
    "getExemptionAuditSwitchById": getExemptionAuditSwitchById,
    "deleteExemptionAuditSwitch": deleteExemptionAuditSwitch,

    "getExemptionApplyConditionUrl": '/api/electionservice/exemptionGradute/applyCondition/list',  //免修条件列表
    "addExemptionApplyCondition": addExemptionApplyCondition,
    "updateExemptionApplyCondition": updateExemptionApplyCondition,
    "getExemptionApplyConditionById": getExemptionApplyConditionById,
    "deleteExemptionApplyCondition": deleteExemptionApplyCondition,
    "downloadApplyConditionTemplate": downloadApplyConditionTemplate,
    "importApplyConditionUrl" : '/api/electionservice/exemptionGradute/applyCondition/import',

    "getExemptionAuditUrl": '/api/electionservice/exemption/findGraduateExemptionApply',  //免修审核列表
    "updateExemptionAuditStatus": updateExemptionAuditStatus,     // 免修审核
    "exportExemptionAudit": exportExemptionAudit,   // 导出审核
    "addExemptionApplyAdmin": addExemptionApplyAdmin,
    "deleteExemptionApplyAdmin": deleteExemptionApplyAdmin,

    "getExemptionApplyCourse": getExemptionApplyCourse,
    "addExemptionApply": addExemptionApply,
    "getMatchedConditionsByCode": getMatchedConditionsByCode,

    "getExemptionStatisticsUrl": '/api/electionservice/exemption/exemptionCount',  //免修免考统计
    "exportExemptionStatistics": exportExemptionStatistics,
    cancelGraduteExemptionApply: cancelGraduteExemptionApply
}
